<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>首页</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <script src="__PUBLIC__/js/jquery-1.9.0.js"></script>
    <script src="__PUBLIC__/home/plugins/layer/layer.js"></script>
    <script src="__PUBLIC__/js/angular.js"></script>
    <!--标准mui.css-->
    <link rel="stylesheet" href="__PUBLIC__/mui/css/mui.css">
    <!--App自定义的css-->
    <link rel="stylesheet" href="__PUBLIC__/css/app.css">
    <link rel="stylesheet" href="__PUBLIC__/css/cart.css">
    <link rel="stylesheet" href="__PUBLIC__/css/common.css">
    <script src="__PUBLIC__/common.js"></script>
</head>
<body ng-app="myApp" ng-controller="Ctrl" ng-cloak>

<!--header-->
<header class="mui-bar mui-bar-nav" >
    <a class="mui-icon mui-icon-left-nav mui-pull-left"style="color: white" href="/index.php/Home/agen/goorder"></a>
    <h1 class="mui-title">确认下单</h1>
</header>

<!--购物车-->
<div class="mui-content cart">
    <ul class="mui-table-view address">

        <li class="mui-table-view-cell mui-media cart-goods" ng-show="address.name ==null">

            <div style=" float: left" class="enter">
                <a href="/index.php/home/agen/address" style="display: block;width: 100%;height: 100%">请添加收货地址</a>
            </div>
        </li>

        <li class="mui-table-view-cell mui-media cart-goods" ng-show="address.name !=null">
            <div class="mui-media-body" style="width: 80%;float: left">
                <span style="width: 90%">{{address.name}}<label style="margin-left: 3em">{{address.phone}}</label></span>
                <span style="width: 90%"><span class="mui-icon mui-icon-location-filled" style="width: 10%"></span><span style="width: 90%">{{address.namepath}}&nbsp;/&nbsp;{{address.address}}</span></span>
            </div>
            <div style="width: 20%;height:42px;float: left" class="enter">
                <a href="/index.php/home/agen/address?flg=2&id={{uid}}&num={{unum}}" style="display: block;width: 100%;height: 100%">＞</a>
            </div>
        </li>
    </ul>

</div>

<div class="mui-input-group" style="margin-top: 3px">
    <div class="mui-input-row"  >
        <label class="time">商品</label>
        <div class="content  "  >
          {{product.name}}
        </div>
    </div>

    <div class="mui-input-row" style="background:white">
        <label class="time">数量</label>
        <div class="content  "  >
           {{num}}
        </div>
    </div>
</div>

<div class="mui-input-group" style=" margin: 20px 20px;">
    <button type="button" class="mui-btn mui-btn-danger mui-btn-block" ng-click="buyNow()">确认下单</button>
</div>

<include file="./Application/Home/View/pub/agentfoot.html"/>
<script>
    var app = getApp("myApp", []);

    app.controller('Ctrl', function($scope,$http) {
        $scope.uid = getUrlParam('id');
        $scope.unum = getUrlParam('num');
        $http({
            method: 'GET',
            url: "/index.php/Home/AgentOnlineorder/getAddressAndProductInfo?id="+$scope.uid+"&num="+$scope.unum
        }).then(function successCallback(response) {
            $scope.num = response.data.data.num;
            $scope.product = response.data.data.product;
            $scope.address = response.data.data.address;
            //console.log(response.data.data);
        }, function errorCallback(response) {

        });

        $scope.buyNow=function () {
            layer.confirm('是否确认下单？', {
                btn: ['取消','确认'] //按钮
            }, function(){
                layer.msg('已取消');
            }, function() {
                if(!$scope.address){
                    layer.msg('请先完善收货地址');
                    return;
                }
                $http({
                    method: 'POST',
                    data: {
                        'num': $scope.num,
                        'productid': $scope.product.id,
                        'addressid': $scope.address.id,
                    },
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    transformRequest: function (obj) {
                        var str = [];
                        for (var p in obj) {
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        }
                        return str.join("&");
                    },
                    url: "/index.php/Home/AgentOnlineorder/addagenorder"
                }).then(function successCallback(response) {
                    //console.log(response.data);
                    if(response.data.code == -1){
                        layer.msg(response.data.msg, {time: 1000});
                    } else if(response.data.code == 0){
                        layer.msg(response.data.data, {time: 1000});
                        setTimeout(function () {
                            window.location.href="/index.php/Home/Agen/goorder"
                        },1000)
                    }
                }, function errorCallback(response) {
                    errorHandle(response);
                });
            })
        }
    })
</script>
</body>
<script src="__PUBLIC__/mui/js/mui.min.js"></script>
</html>